package cn.xuqiudong.backend.module.system.user.service;

import cn.xuqiudong.backend.module.system.user.mapper.SysUserMapper;
import cn.xuqiudong.backend.module.system.user.model.SysUser;
import cn.xuqiudong.common.base.service.BaseService;
import cn.xuqiudong.common.util.encrypt.PasswordUtils;
import org.springframework.stereotype.Service;

/**
 * 功能: :系统用户表 Service
 *
 * @author Vic.xu
 * @since 2025-02-07 16:47
 */
@Service
public class SysUserService extends BaseService<SysUserMapper, SysUser> {

    @Override
    protected boolean hasAttachment() {
        return false;
    }


    /**
     * 根据用户名查询用户
     *
     * @param username username
     * @return SysUser
     */
    public SysUser findByUsername(String username) {
        return mapper.findByUsername(username);
    }


    /**
     * 新增用户的时候 初始化其密码
     */
    @Override
    public int insert(SysUser entity) {
        entity.setPassword(PasswordUtils.entryptPassword(entity.getUsername()));
        return super.insert(entity);
    }

    /**
     * 修改密码
     * @param id
     * @param password
     */
    public Integer updatePassword(Integer id, String password) {
        password = PasswordUtils.entryptPassword(password);
        SysUser user = new SysUser();
        user.setId(id);
        user.setPassword(password);
        return mapper.update(user);
    }
}
